<?php
/**
 * Created by PhpStorm.
 * User: pf
 * Date: 2017/12/6
 * Time: 10:32
 */

namespace App\Repositories;

use App\ApplyDevice;
use App\Device;
use App\DeviceGroup;
use App\Service\WechatDeviceService;


class DeviceRepository
{
    protected $DeviceServer;

    public function __construct()
    {
        $this->DeviceServer = new WechatDeviceService();
    }

    public function getListPage($request)
    {
        // return 111;
        $device= Device::with('spot');
        if($request->searchKey){
            $device=$device->where('comment','like','%'.$request->searchKey.'%')
                ->orWhere('uuid',$request->searchKey)
                ->orWhere('major',$request->searchKey)
                ->orWhere('minor',$request->searchKey)
            ;
        }
       return  $device=$device->paginate($request->limit)->toArray();
    }

    public function store($data)
    {

        $device = Device::insert($data);

        //绑定h5组
        $group=$this->addDeviceGroup();

        $this->bindGroup($group,$data);

        return $device;
    }

    public function storeApplyId($request)
    {
        $deviceService = $this->DeviceServer->applyid($request->quantity, $request->apply_reason, $request->comment);

        if ($deviceService['errcode'] == 0) {

            $data = [
                'quantity' => (int)$request->quantity,
                'apply_reason' => $request->apply_reason,
                'comment' => $request->comment,
                'apply_id' => $deviceService['data']['apply_id'],
                'audit_comment' => $deviceService['data']['audit_comment'],
                'audit_status' => $deviceService['data']['audit_status']
            ];
            //存进applyDevice里面
            $searchParam = [
                "type" => 3,
                "apply_id" => (int)$deviceService['data']['apply_id'],
                "last_seen" => 0,
                "count" => 40
            ];
            //等待下一次微信请求

            sleep(1);

            $deviceSearch = $this->deviceSearch($searchParam);

            foreach ($deviceSearch['data']['devices'] as $k => $v) {
                $deviceSearch['data']['devices'][$k]['apply_id'] = $data['apply_id'];
            }
            ApplyDevice::create($data);

            return $this->store($deviceSearch['data']['devices']);

        } else {

            abort($deviceService['errcode'], $deviceService['errmsg']);
        }
    }

    public function deviceSearch(array $data)
    {
        $search = $this->DeviceServer->search($data);
        if ($search['errcode'] == 0) {
            return $search;
        } else {
            abort($search['errcode'], $search['errmsg']);
        }

    }

    //创建一个H5的device群组//默认
    public function addDeviceGroup(){
        $group=DeviceGroup::first();
        if(!$group){
           $response= $this->DeviceServer->addGroup('huashan');
           if($response['errcode']==0){
               $group=new DeviceGroup();
               $group->group_id=$response['data']['group_id'];
               $group->group_name=$response['data']['group_name'];
               $group->save();
           }
        }
        return $group;
    }
    //绑定设备到组
    public function bindGroup($group,$device){
        $response=$this->DeviceServer->addDeviceToGroup($group->group_id,$device);
        if($response['errcode']==0){
            return true;
        }else{
            return false;
        }
    }
    //根据设备信息得到景点id
    public function deviceByScenic(array $device){
        $device=Device::where('major',$device['major'])->where('minor',$device['minor'])->first();
        if($device){
            return  $device->spot->id ;
        }
        return ' ';
    }

}